import Observable from "XrFrame/core/Observable";

// component/career-selector.ts
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    initValue: { // 属性名
      type: null,
      value: ["",""]
    },
  },
  observers:{
    initValue:function(){
        this.data.OrgAllData.forEach((item,index)=>{
          if(item.text == this.data.initValue[0]){
            this.setData({
              currentSelectedIndex: [index,0],
              currentSelectedValue: [this.data.selectOptions[0][index], this.data.selectOptions[1][0]],
            })
            this.getSelectOptions(index)
          }
        })
        this.data.selectOptions[1].forEach((item,index)=>{
          if(item == this.data.initValue[1]){
            this.setData({
              'currentSelectedIndex[1]': index,
              'currentSelectedValue[1]':  this.data.selectOptions[1][index],
            })
          }
        })
    }
  },
  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show: function () {
      this.getSelectOptions(0);
      this.setData({
        currentSelectedIndex: [0,0],
        currentSelectedValue: [this.data.selectOptions[0][0], this.data.selectOptions[1][0]],
      })
     },
    hide: function () { },
    resize: function () { },
  },
  /**
   * 组件的初始数据
   */
  data: {
    selectOptions:[[""], [""]],
    currentSelectedIndex:[0,0],
    currentSelectedValue:["",""],
    firstLevelOptions:[""],
    OrgAllData:[
      {
        "id":"1",
        "text":"骑士",
        "children":["防护","惩戒","奶骑"]
      },
      {
        "id":"2",
        "text":"战士",
        "children":["武器","狂暴","防护"]
      },
      {
        "id":"3",
        "text":"牧师",
        "children":["神圣","戒律","暗影"]
      },
      {
        "id":"4",
        "text":"DK",
        "children":["鲜血","冰霜","邪恶"]
      },
      {
        "id":"5",
        "text":"法师",
        "children":["冰霜","火焰","奥数"]
      },
      {
        "id":"6",
        "text":"术士",
        "children":["恶魔","毁灭","诅咒"]
      },
      {
        "id":"7",
        "text":"萨满",
        "children":["奶萨","元素","增强"]
      },
      {
        "id":"8",
        "text":"猎人",
        "children":["生存","射击","兽王"]
      },
      {
        "id":"9",
        "text":"小龙人",
        "children":["奶龙","增辉","湮灭"]
      },
      {
        "id":"10",
        "text":"恶魔猎手",
        "children":["复仇","浩劫"]
      },
      {
        "id":"11",
        "text":"武僧",
        "children":["酒仙","踏风","毛线僧"]
      },
      {
        "id":"12",
        "text":"盗贼",
        "children":["战斗","刺杀","投毒"]
      },
    ]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onSupportChange: function(e:any){
      console.info("bindchange is triggered")
      console.info(e.detail)
      //提交数据到父页面
      this.triggerEvent('selectorChanged', {value:this.data.currentSelectedValue}) 
    },
    onSupportColumnChange: function(e:any){
      console.info(e.detail)
      //如果是第一级变了，更改第一级信息，并且获取第二级信息
      if (e.detail.column == 0){
        this.getSelectOptions(e.detail.value)
        this.setData({
          currentSelectedIndex: [e.detail.value,0],
          currentSelectedValue: [this.data.OrgAllData[e.detail.value].text, this.data.selectOptions[1][0]],
        })

      }
      else{
        this.setData({
          'currentSelectedIndex[1]': e.detail.value,
          'currentSelectedValue[1]': this.data.selectOptions[1][e.detail.value],
        })
      }
    },

    getSelectOptions(preIndex:number){
      //设置第一层信息
      if(this.data.firstLevelOptions[0]==""){
        console.log("获取了第一层信息")
        this.data.OrgAllData.forEach((item,index) => {
          if(index == 0){
            this.setData({
              'firstLevelOptions':[item.text]
            })
          }
          else{
            this.setData({
              'firstLevelOptions':this.data.firstLevelOptions.concat(item.text)
            })
          }
         
        })
      }
      //设置第二层信息
      this.data.OrgAllData.forEach((item,index) => {
        if(index == preIndex){
          this.setData({
            'selectOptions':[this.data.firstLevelOptions,item.children]
          })
        }
      })
    }
  }
})